<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>

          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog

      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeAddDialog"
      @open="openDialog"
    >
      <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select></el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="clickSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments, delDepartments } from '@/api/department'
import transTree from '@/utils/transTree'
export default {
  data() {
    const newPwd = (rule, value, callback) => {
      if (this.codeList.includes(value)) {
        callback(new Error('code有重复'))
      } else {
        callback()
      }
    }
    return {
      // 依赖一份树形数据
      list: [{
        label: '财务部',
        children: [
          {
            label: '财务核算部',
            manager: '骂哭欧'
          },
          {
            label: '税务核算部',
            manager: '哭欧'

          }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称必填', tigger: 'blur' },
          { min: 1, max: 50, message: '部门名称必填', tigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码必填', tigger: 'blur' },
          { min: 1, max: 50, message: '部门编码必填', tigger: 'blur' },
          { validator: newPwd, triggers: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人必填', tigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍必填', tigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍必填', tigger: 'blur' }
        ]

      },
      managerList: [],
      codeList: []

    }
  },
  mounted() {
    this.fetchDepartment()
  },

  methods: {
    async fetchDepartment() {
      const res = await getDepartments()
      // console.dir(res)
      // console.dir(res.depts)
      // console.dir(this.list)

      this.list = transTree(res.depts)
      res.depts.forEach(item => {
        // console.dir(item.code)
        this.codeList.push(item.code)
        // console.log(this.codeList)
      })
    },
    addDepart(id) {
      console.log(id)
      this.dialogVisible = true
      this.form.pid = id
    },
    closeAddDialog() {
      this.dialogVisible = false
      this.$refs.deptForm.resetFields()
    },
    async openDialog() {
      const res = await getEmployeeSimple()
      this.managerList = res
    },
    clickSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await updateDepartments(this.form)
          } else {
            await addDepartments(this.form)
          }
          // 提示
          this.$message.success(`${this.form.id ? '编辑' : '新增'}部门成功`)
          // 隐藏窗口
          this.dialogVisible = false
          // 渲染页面
          this.fetchDepartment()
          // 清空里边数据
          this.$refs.deptForm.resetFields()
          // 调用下边方法  让里边数据为空
          this.adclickbmit()
        }
      })
    },
    async editDepart(id) {
      this.dialogVisible = true
      const res = await getDepartDetail(id)
      this.form = res
    },
    adclickbmit() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    },
    delDepart(id) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartments(id)
        this.$message.success('删除成功')
        this.fetchDepartment()
      }).catch(() => {

      })
    }

  }

}
</script>

<style>
</style>

